/**
 * 
 */
package scriptrunner.execution;

import java.io.Serializable;

/**
 * @author Administrator
 * 
 */
public class ListAddElementFunction extends Function implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8292564459004902095L;

	/**
	 * @param id
	 * @param pl
	 */
	public ListAddElementFunction() {
		super(new String("AddToList"), new ParameterList());
		this.pl.add("listObject");
		this.pl.add("newElement");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * scriptrunner.execution.Function#Execute(scriptrunner.execution.Session)
	 */
	@Override
	public ValueObject Execute(Session se) throws ExecuteErrorException {
		ValueObject a1 = se.getVariableValue("listObject");
		ValueObject a2 = se.getVariableValue("newElement");
		if (a1 instanceof ValueListObject) {
			ValueListObject listObject = (ValueListObject) a1;
			try {
				listObject.AddListElement(a2);
			} catch (CloneNotSupportedException e) {
				throw new ExecuteErrorException(
						"The value cannot be cloned when it is assigned to a varible");
			}
		} else
			throw new ExecuteErrorException("The value is not a list");

		return null;
	}

}
